//
// Created by ysbf on 2021/2/15.
//

#ifndef BNO055DRIVER_SRC_MS5611_MS5611_H
#define BNO055DRIVER_SRC_MS5611_MS5611_H

#include "ms5611_reg.h"
#include <cstdint>
#include <cmath>
class MS5611 {
public:

public:

  typedef void (*RWRegCallback)(uint16_t);
  typedef void(*ReadCallback)();
  typedef void (*spi_read_type)(const uint8_t *buffer, uint16_t len);
  typedef void (*spi_write_type)(const uint8_t *buffer, uint16_t len);
  typedef void (*spi_async_read_type)(const uint8_t *buffer, uint16_t len, void callback_fun(uint16_t));
  typedef void (*spi_async_write_type)(const uint8_t *buffer, uint16_t len, void callback_fun(uint16_t));
  typedef void (*delay_type)(uint16_t);

  MS5611(spi_read_type spi_read, spi_write_type spi_write, spi_async_read_type spi_async_read,
         spi_async_write_type spi_async_write, delay_type delay);

  uint32_t cmd_adc(char cmd);


  float getEstimatedAltitude();
  uint32_t getPressure();

  double getPressureCompensated();

  uint32_t getTemperature();

  double getTemperatureCompensated();

  void cmd_reset();

  uint16_t cmd_prom(char coef_num);

  static uint8_t crc4(uint16_t n_prom[]);

  void init();

private:
  spi_read_type m_spi_read;
  spi_write_type m_spi_write;

  spi_async_read_type m_spi_async_read;
  spi_async_write_type m_spi_async_write;

  delay_type m_delay;
  uint8_t spi_buffer[33]{0};

  int _cs;
  uint16_t C[8]; // calibration coefficients
  unsigned char n_crc; // crc value of the prom
};

#endif //BNO055DRIVER_SRC_MS5611_MS5611_H
